package org.jeecg.modules.demo.ipds.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.ipds.entity.ByteFrameReuseZu;
import org.jeecg.modules.demo.ipds.service.IByteFrameReuseZuService;
import org.jeecg.modules.demo.ipds.service.ITreeByteListReuseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description: 子帧组复用库
 * @Author: jeecg-boot
 * @Date:   2024-03-29
 * @Version: V1.0
 */
@Api(tags="子帧组复用库")
@RestController
@RequestMapping("/ipds/byteFrameReuseZu")
@Slf4j
public class ByteFrameReuseZuController extends JeecgController<ByteFrameReuseZu, IByteFrameReuseZuService> {
	@Autowired
	private IByteFrameReuseZuService byteFrameReuseZuService;

	@Autowired
	private ITreeByteListReuseService treeByteListReuseService;
	
	/**
	 * 分页列表查询
	 *
	 * @param byteFrameReuseZu
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "子帧组复用库-分页列表查询")
	@ApiOperation(value="子帧组复用库-分页列表查询", notes="子帧组复用库-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ByteFrameReuseZu>> queryPageList(ByteFrameReuseZu byteFrameReuseZu,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ByteFrameReuseZu> queryWrapper = QueryGenerator.initQueryWrapper(byteFrameReuseZu, req.getParameterMap());
		Page<ByteFrameReuseZu> page = new Page<ByteFrameReuseZu>(pageNo, pageSize);
		IPage<ByteFrameReuseZu> pageList = byteFrameReuseZuService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   通过id删除
	 *
	 * @param
	 * @return
	 */
	@AutoLog(value = "子帧组-通过id,index删除")
	@ApiOperation(value="子帧组-通过id,index删除", notes="子帧组-通过id,index删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="byte_id",required=true) String byte_id,@RequestParam(name="index",required=false) Integer index) {

		//如果选择删除了删除第几个子帧组
		if (index!=null&&!index.equals("")){
			QueryWrapper<ByteFrameReuseZu> queryWrapper=new QueryWrapper<>();
			//根据字节占位符查询出所有子帧组
			queryWrapper.eq("byte_id",byte_id);
			queryWrapper.orderByAsc("index_sx");
			List<ByteFrameReuseZu> ByteFrameReuseZus = byteFrameReuseZuService.list(queryWrapper);
			System.out.println(ByteFrameReuseZus);
			//集合的第几个 index是从1开始的
			ByteFrameReuseZu ByteFrameReuseZu = ByteFrameReuseZus.get(index - 1);
			byteFrameReuseZuService.removeById(ByteFrameReuseZu);
			QueryWrapper<ByteFrameReuseZu> updateQuer = new QueryWrapper<>();

			//删除之后修改一下顺序
			updateQuer.eq("byte_id", byte_id);
			List<ByteFrameReuseZu> byteFrame = byteFrameReuseZuService.list(updateQuer);
			for (ByteFrameReuseZu frameZus : byteFrame) {
				if (frameZus.getIndexSx() > index) {
					Integer indexSx = frameZus.getIndexSx() - 1;
					frameZus.setIndexSx(indexSx);
				}
			}
			byteFrameReuseZuService.updateBatchById(ByteFrameReuseZus);

		}

		//没index全删了
		if (index==null||index.equals("")){
			QueryWrapper<ByteFrameReuseZu> queryWrapper3 = new QueryWrapper<>();
			queryWrapper3.eq("byte_id", byte_id);
			byteFrameReuseZuService.remove(queryWrapper3);
			if (byteFrameReuseZuService.list(queryWrapper3).size() == 0) {
				System.out.println(byteFrameReuseZuService.list(queryWrapper3).size() == 0);
				treeByteListReuseService.removeById(byte_id);
			}
		}


		return Result.OK("删除成功!");
	}


	/**
	 *   添加
	 *
	 * @param byteFrameReuseZu
	 * @return
	 */
	@AutoLog(value = "子帧组复用库-添加")
	@ApiOperation(value="子帧组复用库-添加", notes="子帧组复用库-添加")
	@RequiresPermissions("ipds:byte_frame_reuse_zu:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ByteFrameReuseZu byteFrameReuseZu) {
		byteFrameReuseZuService.save(byteFrameReuseZu);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param byteFrameReuseZu
	 * @return
	 */
	@AutoLog(value = "子帧组复用库-编辑")
	@ApiOperation(value="子帧组复用库-编辑", notes="子帧组复用库-编辑")
	@RequiresPermissions("ipds:byte_frame_reuse_zu:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ByteFrameReuseZu byteFrameReuseZu) {
		byteFrameReuseZuService.updateById(byteFrameReuseZu);
		return Result.OK("编辑成功!");
	}





	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "子帧组复用库-通过id查询")
	@ApiOperation(value="子帧组复用库-通过id查询", notes="子帧组复用库-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ByteFrameReuseZu> queryById(@RequestParam(name="id",required=true) String id) {
		ByteFrameReuseZu byteFrameReuseZu = byteFrameReuseZuService.getById(id);
		if(byteFrameReuseZu==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(byteFrameReuseZu);
	}




    /**
    * 导出excel
    *
    * @param request
    * @param byteFrameReuseZu
    */
    @RequiresPermissions("ipds:byte_frame_reuse_zu:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ByteFrameReuseZu byteFrameReuseZu) {
        return super.exportXls(request, byteFrameReuseZu, ByteFrameReuseZu.class, "子帧组复用库");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("ipds:byte_frame_reuse_zu:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ByteFrameReuseZu.class);
    }

}
